<?php
class Billing_model extends MY_Model
{
	public
		$table = 'billing',
		$prKey = 'id',

		$status = array(			
			'1' => 'Incomplete', 
			'2' => 'Submitted',
			'3' => 'Approved',
			'4' => 'Rejected',
			'5' => 'Released',
		),
		
		$category = array(			
			'1' => 'Company', 
			'2' => 'Contributor',
		),

		$payment_type = array(			
			'1' => 'Annual', 
			'2' => 'Manual',
		),
		
		$filter_balance = array(			
			'1' => 'less 50$', 
			'2' => 'from 50$ to 100$',
			'3' => 'from 100$ to 200$',
			'4' => 'from 200$ to 300$',
			'5' => 'from 300$ to 400$',
			'6' => 'from 400$ to 500$',
			'7' => 'more 500$'
		)		
	;

	function __construct()
	{
		parent::__construct();

		$this->fields = array(
			'id'		 		=> '',
			'id_user' 			=> T_('User Account'),
			'category'			=> T_('Category'),
			'id_account_types'	=> T_('Account Type'), 
			'payment_type' 		=> T_('Kind of Payment'),
			'amount' 			=> T_('Amount Paid'),
			'date_created'		=> T_('Date'),
		);

		$this->rules = array(
			'id_user' 			=> "trim|required|integer",	
			'category' 			=> "trim|is_natural_no_zero",
			'id_account_types'	=> "trim|is_natural_no_zero",	
			'payment_type' 		=> "trim|is_natural_no_zero",				
			'amount' 			=> "trim|required",
			'date_created'		=> "trim|required|max_length[32]",
		);
	}
	
	function search($config = array())
	{
		$config = array_merge(array('rowsOnPage' => 5, 'order' => 'id'), $config);
		
		if ($this->input->post('per_page'))
		{
			$config['rowsOnPage'] = $this->input->post('per_page');
			$per_page = 0;
		}		
		else if ($this->input->get('per_page'))
		{
			$per_page = $this->input->get('per_page');
		}
		else
		{
			$per_page = 0;
		}

		$id_user = (!empty($config['id_user'])) ? $config['id_user'] : $this->input->get_post('id_user');
		$id_recommend = $this->input->get_post('id_recommend');
		$fullname = $this->input->get_post('fullname');
		$email    = $this->input->get_post('email');
		$phone	  = $this->input->get_post('phone');
		$status	  = $this->input->get_post('status');
		$social_status = $this->input->get_post('social_status');
		$gender	  = $this->input->get_post('gender');
		$date_start = $this->input->get_post('date_start');
		$date_end = $this->input->get_post('date_end');
		
		$order 		= $this->input->get_post('order');
		$direction 	= $this->input->get_post('direction');

		// main sql		
		$sql = '';
		$sql .= ' FROM '.$this->table.' b';
		$sql .= ' LEFT JOIN contributors c ON b.id_user = c.id';  
		$sql .= ' WHERE 1 = 1';  

		if ($id_user)
		{
			$sql .= ' AND b.id_user='.$this->db->escape($id_user); 
		}
		
		if ($status)
		{
			$sql .= ' AND b.status='.$this->db->escape($status); 
		}


		if ($date_start && $date_end)
		{
			$sql .= ' AND b.date_created>='.$this->db->escape($date_start). ' AND b.date_created<='.$this->db->escape($date_end); 
		}
		else if ($date_start)
		{
			$sql .= ' AND b.date_created>='.$this->db->escape($date_start); 
		}
		else if ($date_end)
		{
			$sql .= ' AND b.date_created<='.$this->db->escape($date_end); 
		}
		
		switch ($order)
		{
			case 'date':
				$sql .= ' ORDER BY b.date_created'; 
				break;
			
			default:
				$sql .= ' ORDER BY b.id';
				$direction = 'DESC';
		}
		
		$sql .= ($direction == 'DESC') ? ' DESC' : ' ASC';

		// pagination config
		//$config['base_url'] = Xurl_add(Xregenerate_url('per_page,search', true), 'search', $search);
		$config['per_page'] = $config['rowsOnPage'];
		//$config['total_rows'] = $this->getRowCount($sql);

		$this->pagination->initialize($config); 
		
		// main sql
		$sql = "SELECT b.*,c.balance,c.email,CONCAT(c.first_name,' ',c.last_name) AS name_contributor " . $sql;// . sprintf(' LIMIT %d, %d', $per_page, $config['per_page']); 

		$this->data['list'] = $this->db->query($sql)->result();
		$this->data['per_page'] = $config['per_page'];
	}		
	
  function delete($items_serialized = null)
  {
    $items = explode("&", $items_serialized);
    
    $report = "";
    foreach ($items as $id)
    {  
      $res = $this->db->delete($this->table, array($this->prKey => $id));
      
      if (is_null($res))
      {
        $this->log->writeMessage('error', T_('Error. The Test was not removed.'));
        $report .= ",0"; // don't deleted
      }
      else
      {
      	//$this->db->delete('users_numbers', array('id_users' => $id));
      	
        $this->log->writeMessage('done', T_('The Test was removed.'));
        $report .= ",1"; // deleted        
      } 
    }
    return substr($report, 1);
  }
  	
  function insert($data)
  {
    if (!empty($data))
    {  
      $data['date_created'] = date('Y-m-d H:i:s');     
      
      parent::insert($data);

      $insert_id = $this->db->insert_id();
      
      if ($insert_id > 0)
      {
        //$this->log->writeMessage('done', T_('The Test added successfully.'));
		
        return $insert_id;
      }
      else
      {
        //$this->log->writeMessage('error', T_('Error! The Test was not created.'));
        
        return FALSE;
      }
    }   
  }

  function update($data)
  {
    if (!empty($data))
    {
      $data['date_modified'] = date('Y-m-d H:i:s');
        
      $res = parent::update($data);

      $this->log->writeMessage('done', T_('Saved succesfully.'));
		
      return TRUE;      
    }   
  }
  
  function get($id)
  {
  	$sql  = ' FROM '.$this->table.' t';
	$sql .= ' LEFT JOIN contributors c ON t.id_contributor = c.id';  
	$sql .= ' WHERE t.id='.$id;
    $sql = "SELECT t.*,CONCAT(c.first_name,' ',c.last_name) AS name_contributor " . $sql;
    return $this->db->query($sql)->row_array();
  }
}
?>